//
//  DriverOrderViewController.m
//  Park
//
//  Created by 123456 on 2017/11/6.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "DriverOrderViewController.h"
#import "DriverOrderTableViewCell.h"
#import "MJRefresh.h"
#import "DriverOrderViewModel.h"
#import "DriverOrderModel.h"
#import "ConfirmCancelViewModel.h"
@interface DriverOrderViewController ()<UITableViewDataSource,UITableViewDelegate,DriverCellDelegate,NSObject>
{
    // 需要其他控件控制刷新
    MJRefreshNormalHeader* header;
    MJRefreshBackNormalFooter* footer;
    int _page;
    BOOL RefreshStatus;
    int b;//请求变量

}

@property (nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation DriverOrderViewController


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [header beginRefreshing];

    [[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"Order"];

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self.DriverOrderTableView registerNib:[UINib nibWithNibName:@"DriverOrderTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.DriverOrderTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.title = @"订单";
    
    [self refreshTableView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeplace1:) name:@"Reserve" object:nil];

    self.DriverOrderTableView.estimatedRowHeight = 227;
    self.DriverOrderTableView.rowHeight = UITableViewAutomaticDimension;

    
}


- (void)refreshTableView {
    
    header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 0;
        [self RequestData:_page];
        RefreshStatus = YES;
    }];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    [header beginRefreshing];
    self.DriverOrderTableView.header = header;
    
    footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        b=_page+19;
        b++;
        _page = b;
        [self RequestData:_page];
        RefreshStatus= NO;
    }];
    self.DriverOrderTableView.footer = footer;

}

-(void)RequestData:(int)page{

    
    [DriverOrderViewModel requestWithUrlForDriverOrder:self andParam:@{@"iDisplayStart":@(page),@"iDisplayLength":@"20",@"token":[NSUD objectForKey:@"token"]} success:^(NSMutableArray *model) {
    
    [header endRefreshing];
    [footer endRefreshing];
    
    NSMutableArray *scheduingAry = [DriverOrderModel mj_objectArrayWithKeyValuesArray:model];

    if (RefreshStatus ==YES) {

        [self.dataArr removeAllObjects];
        
        for (NSDictionary *dic  in scheduingAry) {
            [self.dataArr addObject: dic];
        }

    }else{

        for (NSDictionary *dic in scheduingAry) {
            [self.dataArr addObject: dic];
        }
    }
    
    [self.DriverOrderTableView reloadData];
    
    
} failure:^(NSString *error) {
    
    [header endRefreshing];
    [footer endRefreshing];
    [MBProgressHUD showError:error toView:self.view];
    
}];


}



#pragma mark -  TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return UITableViewAutomaticDimension;
}


- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    
    
    DriverOrderTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    DriverOrderModel *list = self.dataArr[indexPath.row];
    
    
    
    
    if ([list.status isEqualToString:@"0"]) {
        
        cell.CancelBUtton.alpha = 1;
        cell.ConfirmBUtton.alpha = 1;
        cell.ConfirmLabel.alpha = 0;

    }else if([list.status isEqualToString:@"1"]){
        
        cell.CancelBUtton.alpha = 0;
        cell.ConfirmBUtton.alpha = 0;
        cell.ConfirmLabel.alpha=1;
        cell.ConfirmLabel.text = @"已确认";
        cell.ConfirmLabel.textColor =RGB(40, 130, 60, 1.0);
        
    }else if([list.status isEqualToString:@"-1"]){
        cell.CancelBUtton.alpha = 0;
        cell.ConfirmBUtton.alpha = 0;
        cell.ConfirmLabel.alpha=1;
        cell.ConfirmLabel.text = @"已取消";
        cell.ConfirmLabel.textColor = RGB(193, 33, 71, 1.0);

        
    }
    
    
    cell.OrderTimelabel.text = [NSString stringWithFormat:@"下单时间：%@",list.operateTime];
    cell.NameLabel.text = [NSString stringWithFormat:@"用户：%@",list.userName];
    cell.PhoneLabel.text = [NSString stringWithFormat:@"用户电话：%@",list.userPhone];
    cell.CarLabel.text = [NSString stringWithFormat:@"车型：%@",list.carName];
    cell.MoneyLabel.text = list.momey;
    cell.LicensePlateLabel.text = [NSString stringWithFormat:@"车牌：%@",list.carNumber];
    cell.TimerLabel.text = [NSString stringWithFormat:@"预约时间：%@",list.scheduledId];
    cell.AddressLabel.text = [NSString stringWithFormat:@"上车点：%@",list.place ];
    cell.MoneyLabel.text = [NSString stringWithFormat:@"积分：%@",list.momey];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate =self;
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
    
}




//确定订单
-(void)confirm1:(UIButton *)button{
   
    
    DriverOrderModel *list = self.dataArr[[self.DriverOrderTableView indexPathForCell:((DriverOrderTableViewCell *)[[button   superview]superview])].row];


    NSLog(@"MyRow:%@",list.id);
    
    
    
    [self reserveAffirm:list.id state:0];
    
    
    
}


//取消订单
-(void)cancel1:(UIButton *)button {
    
    NSLog(@"MyRow:%ld",[self.DriverOrderTableView indexPathForCell:((DriverOrderTableViewCell *)[[button   superview]superview])].row);
    
    DriverOrderModel *list = self.dataArr[[self.DriverOrderTableView indexPathForCell:((DriverOrderTableViewCell *)[[button   superview]superview])].row];
    
    
    
    [self reserveAffirm:list.id state:1];
    
    
}


-(void)reserveAffirm:(NSString *)id state:(int)i
{
    
    [ConfirmCancelViewModel requestWithUrlForConfirmCancel:self andParam:@{@"reserveId":@(id.integerValue),@"state":@(i)} success:^(NSString *model) {
        
        [header beginRefreshing];
        

        
    } failure:^(NSString *error) {
        
        [MBProgressHUD showError:error toView:self.view];
        
    }];

}



-(void)changeplace1:(NSNotification *)notification{
    

    [MBProgressHUD showSuccess:@"收到一条新订单" toView:self.view];
    
    [self RequestData:1];
}




-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}


@end
